<?php

require FCPATH . '/application/controllers/frontend.php';

class Home extends Frontend {

	public function __construct() {
		parent::__construct();
	}

	public function index() {
		$lang = $this->get_language();
		$this->load->helper('text');
		$this->load->model(array('slide_show_model', 'project_model', 'news_model'));
		$this->load_css('frontend_home_index');
		$this->load_js('frontend_home_index');

		if ($lang == 'zh') {
			$words = array(170, 125, 360, 200, 265, 280);
		} else {
			$words = array(250, 185, 450, 270, 305, 305);
		}

		$slide_show_columns = array(
			'link',
			array('n.name', 'image_name'),
			array('description_' . $lang, 'description')
		);
		$project_columns = array(
			array('id', 'id'),
			array('n.name', 'cover_name'),
			array('name_' . $lang, 'name'),
			array('summary_' . $lang, 'summary')
		);
		$news_columns = array(
			'id', 'created_at',
			array('nh.name', 'cover_name'),
			array('title_' . $lang, 'title'),
			array('content_' . $lang, 'content')
		);

		$this->load->model('kvs_model');
		$newsIds = $this->kvs_model->getNewsAtHome();
		$news_conditions = array(
			array('where_in', 'id', $newsIds)
		);
		$newsData = $this->news_model->records($news_columns, $news_conditions);
		$news = array();
		foreach ($newsData as $item) {
			$key = array_search($item->id, $newsIds);
			$news[$key] = $item;
		}

		$this->rend('home_index', array(
			'slide_shows' => $this->slide_show_model->records($slide_show_columns, array(array('where', 'type', 1))),
			'projects' => $this->project_model->records($project_columns, array(array('where', 'home_display', '1')), array(array('created_at', 'desc')), 8),
			'news' => $news,
			'words' => $words
		));
	}

}
